<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="批量状态" prop="batchSts">
        <el-select v-model="queryParams.batchSts" placeholder="请选择批量状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.COST004"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="缴费时间" prop="costDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.costDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择缴费时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          size="mini"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
        </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          size="mini"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          :disabled="multiple"
          @click="handleExecute"
          size="mini"
        >执行</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="costBatchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="批量id" align="center" prop="batchId" />
      <el-table-column label="批量名称" align="center" prop="batchName" />
      <el-table-column label="批量状态" align="center" prop="batchSts">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.COST004" :value="scope.row.batchSts"/>
        </template>
      </el-table-column>
      <el-table-column label="小区" align="center" prop="areaName" />
      <el-table-column label="缴费户数" align="center" prop="doorSum" />
      <el-table-column label="缴费总金额" align="center" prop="moneySum" />
      <el-table-column label="缴费时间" align="center" prop="costDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.costDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收费员" align="center" prop="creater" />
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleSelect(scope.row)" :disabled="scope.row.batchSts!=1"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改批量缴费对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="批量名称" prop="batchName">
          <el-input v-model="form.batchName" placeholder="请输入批量名称" />
        </el-form-item>
        <el-form-item label="小区" prop="areaId">
          <el-select v-model="form.areaId" placeholder="请输入小区" clearable :style="{width: '100%'}" :disabled="form.batchSts==1">
            <el-option v-for="(item, index) in areaIdOptions" :key="index" :label="item.areaName"
              :value="item.areaId" ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看明细 -->
      <el-dialog :title="titleDetail" :visible.sync="openDetail" width="500px" append-to-body>
          <el-table v-loading="loading" :data="costBatchDetailList" >
            <el-table-column label="小区" align="center" prop="areaName" />
            <el-table-column label="楼宇" align="center" prop="groupName" />
            <el-table-column label="单元" align="center" prop="unitName" />
            <el-table-column label="户" align="center" prop="doorName" />
            <el-table-column label="房屋编号" align="center" prop="houseId" />
            <el-table-column label="姓名" align="center" prop="userName" />
            <el-table-column label="缴费金额" align="center" prop="costMoney" />
          </el-table>
      </el-dialog>

      <el-dialog :title="titleExecute" :visible.sync="openExecute" width="500px" append-to-body>
        <el-form ref="form" :model="formExecute" :rules="rules" label-width="80px">
          <el-form-item label="缴费时间" prop="costDate">
            <el-date-picker clearable
              v-model="formExecute.costDate" :style="{width: '100%'}"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择缴费时间" :disabled="form.batchSts==1">
            </el-date-picker>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitExecuteForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCostBatch, getCostBatch, delCostBatch, addCostBatch, updateCostBatch,getAreaInfo,listCostBatchDetail,executeCostBatch } from "@/api/fund/costBatch";

export default {
  name: "CostBatch",
  dicts: ['COST004'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalDetail: 0,
      // 批量缴费表格数据
      costBatchList: [],
      costBatchDetailList: [],
      // 弹出层标题
      title: "",
      titleDetail: "",
      // 是否显示弹出层
      open: false,
      openDetail: false,
      openExecute: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchSts: null,
        areaId: null,
        costDate: null,
        createDate: null,
      },
      areaIdOptions:[{}],
      // 表单参数
      form: {},
      formExecute: {},
      // 表单校验
      rules: {
        costDate: [
          { required: true, message: "缴费时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.areaSelect();
  },
  methods: {
    areaSelect(){
        getAreaInfo().then(response => {
          this.areaIdOptions = response.rows;
        });
      },
    /** 查询批量缴费列表 */
    getList() {
      this.loading = true;
      listCostBatch(this.queryParams).then(response => {
        this.costBatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleSelect(row){
    this.openDetail = true
      listCostBatchDetail(row.batchId).then(response => {
        this.costBatchDetailList = response.rows;
        this.totalDetail = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openExecute = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        batchId: null,
        batchName: null,
        batchSts: null,
        areaId: null,
        doorSum: null,
        moneySum: null,
        costDate: null,
        creater: null,
        createDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.multiple = !selection.length;
      selection.map(item => {
        if(item.batchSts==1){
          this.multiple  = true;
        }
      })
      this.ids = selection.map(item => item.batchId);
      this.titleDetail = "查看批量缴费明细";
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加批量缴费";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const batchId = row.batchId || this.ids
      getCostBatch(batchId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改批量缴费";
      });
    },
    /** 修改按钮操作 */
    handleExecute(row) {
      this.reset();
      this.openExecute = true;
      this.titleExecute = "执行批量缴费";

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.batchId != null) {
            updateCostBatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCostBatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 提交按钮 */
    submitExecuteForm() {
      executeCostBatch(this.ids,this.formExecute.costDate).then(response => {
          this.$modal.msgSuccess("执行成功");
          this.openExecute = false;
          this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const batchIds = row.batchId || this.ids;
      this.$modal.confirm('是否确认删除批量缴费编号为"' + batchIds + '"的数据项？').then(function() {
        return delCostBatch(batchIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fund/costBatch/export', {
        ...this.queryParams
      }, `costBatch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang='scss'>
  .left-board {
    width: 500px;
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
  }
</style>
